package com.google.dagger2.mvp.view;

import android.os.Handler;

import com.google.dagger2.mvp.bean.User;
import com.google.dagger2.mvp.biz.OnloginListener;
import com.google.dagger2.mvp.biz.UserBiz;

/**
 * Created by Administrator on 2017/10/25.
 */
public class UserLoginPresenter {


    private final IUserLoginView userLoginView;
    private final UserBiz userBiz;
    private Handler handler = new Handler();

    public UserLoginPresenter(IUserLoginView userLoginView) {
        this.userLoginView=userLoginView;
        this.userBiz = new UserBiz();
    }

    public void login() {
        userLoginView.ShowLoading();
        userBiz.login(userLoginView.getUserName(), userLoginView.getPassword(), new OnloginListener() {
            @Override
            public void loginSuccess(final User user) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        userLoginView.toMainActivity(user);
                        userLoginView.hideLoading();
                    }
                });
            }

            @Override
            public void loginFailed() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        userLoginView.showFailError();
                        userLoginView.hideLoading();
                    }
                });
            }
        });

    }

    public void clear() {
        userLoginView.clearUserName();
        userLoginView.clearPassWord();
    }
}
